{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# project_scheduling_sat"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/project_scheduling_sat.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/project_scheduling_sat.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "from ortools.sat.python import cp_model\n",
    "\n",
    "#project name, duration, starts earliest, ends latest, demand role A, demand role S, demand role J\n",
    "projects = [\n",
    "            ['P1',3, 0, 9, 1, 0, 1],\n",
    "            ['P2',8, 0, 9, 1, 1, 0],\n",
    "            ['P3',3, 0, 9, 1, 0, 2],\n",
    "            ['P4',4, 0, 9, 1, 0, 1]\n",
    "            ]\n",
    "\n",
    "num_projects = len(projects)\n",
    "\n",
    "roles = ['A','S','J']\n",
    "\n",
    "#Roles available at each time step\n",
    "available_roles = [\n",
    "    [2,2,2,2,2,2,2,2,2,2], #Role A\n",
    "    [1,1,1,1,1,1,1,1,1,1], #Role S\n",
    "    [1,1,1,1,1,1,1,2,2,2]  #Role J\n",
    "]\n",
    "\n",
    "all_projects = range(num_projects)\n",
    "all_roles = range(len(roles))\n",
    "\n",
    "# Creates the model.\n",
    "model = cp_model.CpModel()\n",
    "\n",
    "#Creating decision variables\n",
    "\n",
    "#starts and ends of the projects\n",
    "starts = [model.NewIntVar(projects[j][2], projects[j][3] + 1 , 'start_%i' % j) for j in all_projects]\n",
    "ends = [model.NewIntVar(projects[j][2], projects[j][3] + 1, 'end_%i' % j) for j in all_projects]\n",
    "intervals = [model.NewIntervalVar(starts[j], projects[j][1], ends[j], 'interval_%i' % j) for j in all_projects]\n",
    "\n",
    "# Role A has a capacity 2. Every project uses it.\n",
    "demands = [1 for _ in all_projects]\n",
    "model.AddCumulative(intervals, demands, 2)\n",
    "\n",
    "# Role S has a capacity of 1\n",
    "model.AddNoOverlap([intervals[i] for i in all_projects if projects[i][5]])\n",
    "\n",
    "# Project J has a capacity of 1 or 2.\n",
    "used_capacity = model.NewIntervalVar(0, 7, 7, 'unavailable')\n",
    "intervals_for_project_j = intervals + [used_capacity]\n",
    "demands_for_project_j = [projects[j][6] for j in all_projects] + [1]\n",
    "model.AddCumulative(intervals_for_project_j, demands_for_project_j, 2)\n",
    "\n",
    "#We want the projects to start as early as possible\n",
    "model.Minimize(sum(starts))\n",
    "\n",
    "# Solve model.\n",
    "solver = cp_model.CpSolver()\n",
    "solver.parameters.log_search_progress = True\n",
    "status=solver.Solve(model)\n",
    "\n",
    "for it in zip(starts, ends):\n",
    "    print(\"[%i, %i]\" % (solver.Value(it[0]), solver.Value(it[1])))\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
